package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Feedback;
import com.qianniu.napi.admin.service.IFeedbackService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-04
 */
@Controller
public class FeedbackController {

    private static final Logger logger = LoggerFactory.getLogger(FeedbackController.class);

    @Autowired
    private IFeedbackService feedbackService;

    @RequiresPermissions("admin:user:view")
    @RequestMapping("admin/feedback/index")
    public String index() {
        return "admin/feedback";
    }

    @RequiresPermissions("admin:user:view")
    @RequestMapping("/admin/feedback/list")
    @ResponseBody
    public Page<Feedback> alist(@RequestParam(value = "offset", required = false) Integer offset,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "search", required = false) String search,
                                @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Feedback> wrapper = new EntityWrapper<Feedback>();
        int utype = ObjectUtil.getUserType();
        Long sid = ObjectUtil.getSid();
        if (utype == 99) wrapper.eq("mid", ObjectUtil.getMid());
        else if(sid != null && sid > 0) wrapper.eq("sid", sid);
        else wrapper.eq("mpid", ObjectUtil.getMpid());

        if (status != null && status != 99) wrapper.eq("ftype", status);
        if (!Strings.isNullOrEmpty(search))
            wrapper.like("phone", search);

        wrapper.orderBy("id", false);
        Page<Feedback> page = feedbackService.selectPage(new Page<Feedback>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/feedback/add")
    public String add(Feedback bean) {
        // 字段校验
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/feedback";
    }

    @RequestMapping("/admin/feedback/getFeedbackById")
    @ResponseBody
    public Feedback getFeedbackById(long id) {
        Feedback feedback = feedbackService.selectById(id);
        return feedback;
    }

    @RequestMapping("/admin/feedback/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = feedbackService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/feedback/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Feedback bean = new Feedback();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        feedbackService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/feedback/modify")
    public String update(Feedback bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = feedbackService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }
}
